package com.lmx.persistence.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lmx.common.context.ActionContextHolder;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.CustomerActivityPackage;
import com.lmx.repoDomain.tmodel.CustomerActivityPackageItem;
import com.lmx.repoDomain.tmodel.CustomerActivityProject;
import com.lmx.repoDomain.tmodel.CustomerActivityProjectItem;

/**
 * 活动套餐service
 *
 * @author ytche
 * @date 2017年7月20日
 *
 */
@Service
@Scope("prototype")
public class CustomerActivityProjectService<T> {
	
	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	protected GenericService dslJpaService;
	
	@Autowired
	protected CommonService commonService;
	
	/**
	 * 获取getCustomerActivityProjectByName
	 */
	public CustomerActivityProject getCustomerActivityProjectByName(String name) {
		StringBuilder query = new StringBuilder(
				"select c from CustomerActivityProject c where c.name = ?1 and c.delStatus != ?2 and c.tenant.id=?3");
		List resultList = this.em.createQuery(query.toString()).setParameter(1, name)
				.setParameter(2, (short) 1)
				.setParameter(3, ActionContextHolder.getContext().getTenantId()).getResultList();
		CustomerActivityProject u = null;
		if (resultList.size() > 0) {
			u = (CustomerActivityProject) resultList.get(0);
		}
		return u;
	}
	
	/**
	 * @author lgl 删除Project 2017年7月19日
	 */
	@Transactional
	public void deleteNursingProject(CustomerActivityProject project) {
		StringBuilder queryItem = new StringBuilder(
				"update  CustomerActivityProjectItem rp set rp.delStatus=1 where rp.project.id = ?1 and rp.delStatus=?2 ");
		this.em.createQuery(queryItem.toString()).setParameter(1, project.getId())
		.setParameter(2, (short) 0).executeUpdate();
		StringBuilder queryProject = new StringBuilder(
				"update  CustomerActivityProject rp set rp.delStatus=1 where rp.id = ?1 ");
		this.em.createQuery(queryProject.toString()).setParameter(1, project.getId())
		.executeUpdate();
	}
	
	
	/**
	 * 验证projectitem是否和plandetail有关联
	 */
	public int checkProjectItemRelations(Customer customer, List<Long> itemIds, int dateTime) {
		
		List modelList = new ArrayList<>(); // 检查护理模版是否包含
		StringBuilder queryDetail = new StringBuilder(
				"select n from CustomerActivityPlanServiceItem n where n.delStatus != ?1 and n.customer.id = ?2  and n.item.id in (:ids) ");
		modelList = this.em.createQuery(queryDetail.toString()).setParameter(1, (short) 1)
				.setParameter(2, customer.getId()).setParameter("ids", itemIds).getResultList();
		
		return modelList.size();
	}
	
	// 根据活动方案 建立 老人活动方案 将level对应的serviceItem copy到project中
	@Transactional
	public void createCustomerProjectByCustomerActivityPackage(Customer customer, CustomerActivityPackage acitivityPackage, CustomerActivityProject project) {
		project.setCustomer(customer);
		project.setAcitivityPackage(acitivityPackage);
		project.setName(customer.getName() + "的活动方案");
		project.setCreateUser(ActionContextHolder.getContext().getUser());
		dslJpaService.setEntityClass(CustomerActivityProject.class);
		dslJpaService.create(project);
		StringBuilder queryItem = new StringBuilder(
				"select h from CustomerActivityPackageItem h where h.delStatus != ?1 and h.acitivityPackage.id=?2 ");
		List<CustomerActivityPackageItem> itemList = this.em.createQuery(queryItem.toString())
				.setParameter(1, (short) 1).setParameter(2, acitivityPackage.getId()).getResultList();
		if (itemList != null && itemList.size() > 0) {
			List<CustomerActivityProjectItem> dataList = new ArrayList<>();
			for (CustomerActivityPackageItem levelItem : itemList) {
				if (levelItem != null) {
					CustomerActivityProjectItem projectServiceItem = new CustomerActivityProjectItem();
					projectServiceItem.setProject(project);
					projectServiceItem.setFrequency(levelItem.getFrequency());
					projectServiceItem.setItem(levelItem.getActivityServiceItem());
					projectServiceItem.setPeriod(levelItem.getPeriod());
					projectServiceItem.setDelStatus((short) 0);
					projectServiceItem.setTenant(customer.getTenant());
					dataList.add(projectServiceItem);
				}
			}
			if (dataList.size() > 0) {
				commonService.batchInsert(dataList);
			}
		}
	}
	
}
